package com.funo.main.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.funo.common.pojo.PageResult;
import com.funo.common.pojo.QueryCondition;
import com.funo.common.pojo.TreeNode;
import com.funo.common.utils.Constant;
import com.funo.main.dao.OrgDao;
import com.funo.main.entities.Org;
import com.funo.main.service.OrgService;

/**
 * 机构业务接口实现类
 * 
 * @author zhengjs
 *
 */
@Service
@Transactional
public class OrgServiceImpl implements OrgService{

	@Autowired
	private OrgDao orgDao;//机构管理DAO
	
	@Override
	public TreeNode orgTree(String id) {
		
		TreeNode root = new TreeNode();
		
		root.setChildren(modules2TreeNodes(orgDao.getMenu()));
		
		for (TreeNode treeNode : root.getChildren()) {
			treeNode.setChildren(modules2TreeNodes(orgDao.getMenu(treeNode.getId())));
		}
		
		return root;
	}
	
	public List<TreeNode> modules2TreeNodes(List<Org> orgs) {
		
		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		
		TreeNode treeNode;
		
		for (Org org : orgs) {
			treeNode = new TreeNode(org.getId(), org.getName(), null, org.getOrgLevel(), null, true);
			treeNodes.add(treeNode);
		}
		
		return treeNodes;
	}

	@Override
	public PageResult<Org> getOrgPage(QueryCondition condition) {
		
		PageResult<Org> page = new PageResult<Org>();
		
		page.setList(orgDao.pageList(condition));
		page.setTotal(orgDao.count(condition));
		
		return page;
	}

	@Override
	public Integer addOrg(Org org, String parentID) {

		Integer type=Constant.STATUS_ABNORMAL;//初始为0
		
		Org orgTl=orgDao.getOrg(org.getName());//通过机构名,获取机构信息.

		if(orgTl !=null && orgTl.getId() !=null){//重复
			type = Constant.STATUS_REPEAT;//重复标识,2
		}else{
			
			org.setId(UUID.randomUUID().toString());//主键ID
			
			if(parentID=="" || parentID.equals("")){
				org.setParentOrg(null);//父类模块ID
				org.setOrgLevel("N");//不是叶子结点
			}else{
				Org orgChild=orgDao.get(parentID);//通过ID,获取模块父类机构信息.
				org.setParentOrg(orgChild);
				org.setOrgLevel("Y");//是叶子结点
			}
			
			orgDao.save(org);//保存操作
			
			type = Constant.STATUS_SUSSESS;//成功标识,1
		}
		
		return type;
	}

	@Override
	public Integer updateOrg(Org org) {
		
		Integer type = Constant.STATUS_ABNORMAL;
		
		//通过机构名称和主键id，查询机构信息。
		Org orgTil = orgDao.findOrgByName(org.getName(), org.getId());
		
		if(orgTil !=null && orgTil.getId() !=null){//重复
			type = Constant.STATUS_REPEAT;
		}else{
			//通过id，查询机构信息。
			Org oldOrg = orgDao.get(org.getId());
			
			oldOrg.setName(org.getName());//名称
			oldOrg.setOrderNum(org.getOrderNum());//排列序号
			oldOrg.setCode(org.getCode());//编码
			
			Org parentOrg = oldOrg.getParentOrg();
			
			if(parentOrg!=null){//叶子结点
				oldOrg.setParentOrg(parentOrg);
			}else{//父类节点
				oldOrg.setParentOrg(null);
			}
			
			oldOrg.setComments(org.getComments());//组织简介
			
			orgDao.update(oldOrg);//更新操作
			
			type = Constant.STATUS_SUSSESS;
		}

		return type;
	}

	@Override
	public List<Org> findParentOrg(String id) {
		return orgDao.getMenu(id);
	}

	@Override
	public Org getOrg(String id) {
		return orgDao.get(id);
	}

	@Override
	public void deleteOrg(Org org) {
		orgDao.delete(org.getId());
	}	
}
